Imports System.Data.SqlClient

Public Class cls_listaprecioarticulo_

    Implements ICloneable

#Region "Variables ..."
    Private int_idlistaprecioarticulo As Integer = 0
    Private int_idlistaprecio As Integer = 0
    Private int_idarticulo As Integer = 0
    Private dbl_valor As Double = 0
    Private int_idmoneda As Integer = 0
    Private dec_costo As Decimal = 0
    Private str_formula As String = ""
    Private int_tipo As cls_alteracion_.enu_tipo = cls_alteracion_.enu_tipo.Valor

    Private obj_articulo As cls_articulo_
    Private obj_moneda As cls_moneda_
    Private obj_listaprecio As cls_listaprecio_

    Private col_Items As Collections.Generic.List(Of cls_listaprecioarticulo_) = Nothing
#End Region

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idlistaprecioarticulo
        End Get
    End Property
    Property idlistaprecioarticulo() As Integer
        Get
            idlistaprecioarticulo = int_idlistaprecioarticulo
        End Get
        Set(ByVal value As Integer)
            int_idlistaprecioarticulo = value
        End Set
    End Property
    Property idlistaprecio() As Integer
        Get
            idlistaprecio = int_idlistaprecio
        End Get
        Set(ByVal value As Integer)
            int_idlistaprecio = value
        End Set
    End Property
    Property idarticulo() As Integer
        Get
            idarticulo = int_idarticulo
        End Get
        Set(ByVal value As Integer)
            int_idarticulo = value
        End Set
    End Property
    ReadOnly Property nombreArticulo As String
        Get
            If Not IsNothing(articulo) Then
                Return articulo.ToString
            Else
                Return ""
            End If
        End Get
    End Property
    Property valor() As Double
        Get
            valor = dbl_valor
        End Get
        Set(ByVal value As Double)
            dbl_valor = value
        End Set
    End Property
    Property idmoneda() As Integer
        Get
            idmoneda = int_idmoneda
        End Get
        Set(ByVal value As Integer)
            int_idmoneda = value
        End Set
    End Property
    Property costo As Decimal
        Get
            costo = dec_costo
        End Get
        Set(ByVal value As Decimal)
            dec_costo = value
        End Set
    End Property
    Property formula() As String
        Get
            formula = str_formula
        End Get
        Set(ByVal value As String)
            str_formula = value
        End Set
    End Property
    Property tipo() As cls_alteracion_.enu_tipo
        Get
            tipo = int_tipo
        End Get
        Set(ByVal value As cls_alteracion_.enu_tipo)
            int_tipo = value
        End Set
    End Property
    Property articulo() As cls_articulo_
        Get
            If IsNothing(obj_articulo) Then
                obj_articulo = New cls_articulo_
                If Not obj_articulo.Buscar(idarticulo) Then
                    obj_articulo = Nothing
                End If
            End If
            articulo = obj_articulo
        End Get
        Set(ByVal value As cls_articulo_)
            obj_articulo = value
        End Set
    End Property
    Property moneda() As cls_moneda_
        Get
            If IsNothing(obj_moneda) Then
                obj_moneda = New cls_moneda_
                If Not obj_moneda.Buscar(idmoneda) Then
                    obj_moneda = Nothing
                End If
            End If
            moneda = obj_moneda
        End Get
        Set(ByVal value As cls_moneda_)
            obj_moneda = value
        End Set
    End Property
    Property listaprecio() As cls_listaprecio_
        Get
            If IsNothing(obj_listaprecio) Then
                obj_listaprecio = New cls_listaprecio_
                If Not obj_listaprecio.Buscar(idlistaprecio) Then
                    obj_listaprecio = Nothing
                End If
            End If
            listaprecio = obj_listaprecio
        End Get
        Set(ByVal value As cls_listaprecio_)
            obj_listaprecio = value
        End Set
    End Property
    Property Items() As Collections.Generic.List(Of cls_listaprecioarticulo_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_listaprecioarticulo_))
            col_Items = value
        End Set
    End Property
#End Region

    Public Overrides Function ToString() As String
        Return idlistaprecioarticulo
    End Function
    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_listaprecioarticulo_)
        While obj_Reader.Read()
            Dim Elemento As New cls_listaprecioarticulo_
            Elemento.int_idlistaprecioarticulo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idlistaprecioarticulo")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idlistaprecio"))) = False Then
                Elemento.int_idlistaprecio = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idlistaprecio")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idarticulo"))) = False Then
                Elemento.int_idarticulo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idarticulo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("valor"))) = False Then
                Elemento.dbl_valor = (obj_Reader.GetValue(obj_Reader.GetOrdinal("valor")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmoneda"))) = False Then
                Elemento.int_idmoneda = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmoneda")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("costo"))) = False Then
                Elemento.dec_costo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("costo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("formula"))) = False Then
                Elemento.str_formula = (obj_Reader.GetValue(obj_Reader.GetOrdinal("formula")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo"))) = False Then
                Elemento.int_tipo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idlistaprecioarticulo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idlistaprecioarticulo")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idlistaprecio"))) = False Then
            int_idlistaprecio = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idlistaprecio")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idarticulo"))) = False Then
            int_idarticulo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idarticulo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("valor"))) = False Then
            dbl_valor = (obj_Reader.GetValue(obj_Reader.GetOrdinal("valor")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmoneda"))) = False Then
            int_idmoneda = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmoneda")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("costo"))) = False Then
            dec_costo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("costo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("formula"))) = False Then
            str_formula = (obj_Reader.GetValue(obj_Reader.GetOrdinal("formula")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo"))) = False Then
            int_tipo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo")))
        End If
    End Sub

    Public Function Nuevo(Optional ByVal ObtenerID As Boolean = False) As Boolean
        Nuevo = False
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "listaprecioarticulo__Nuevo"
                .Parameters.Add("@idlistaprecio", SqlDbType.Int).Value = int_idlistaprecio
                .Parameters.Add("@idarticulo", SqlDbType.Int).Value = int_idarticulo
                .Parameters.Add("@valor", SqlDbType.Decimal).Value = dbl_valor
                .Parameters.Add("@idmoneda", SqlDbType.Int).Value = int_idmoneda
                .Parameters.Add("@costo", SqlDbType.Decimal).Value = dec_costo
                .Parameters.Add("@formula", SqlDbType.NVarChar).Value = str_formula
                .Parameters.Add("@tipo", SqlDbType.Int).Value = int_tipo
                .ExecuteNonQuery()
            End With
            Nuevo = True
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Crear el articulo de listaprecioarticulo_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Function

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "listaprecioarticulo__Modificar"
                .Parameters.Add("@idlistaprecioarticulo", SqlDbType.Int).Value = int_idlistaprecioarticulo
                .Parameters.Add("@idlistaprecio", SqlDbType.Int).Value = int_idlistaprecio
                .Parameters.Add("@idarticulo", SqlDbType.Int).Value = int_idarticulo
                .Parameters.Add("@valor", SqlDbType.Decimal).Value = dbl_valor
                .Parameters.Add("@idmoneda", SqlDbType.Int).Value = int_idmoneda
                .Parameters.Add("@costo", SqlDbType.Decimal).Value = dec_costo
                .Parameters.Add("@formula", SqlDbType.NVarChar).Value = str_formula
                .Parameters.Add("@tipo", SqlDbType.Int).Value = int_tipo
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Modificar el articulo de listaprecioarticulo_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idlistaprecioarticulo As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "listaprecioarticulo__Eliminar"
                .Parameters.Add("@idlistaprecioarticulo", SqlDbType.Int).Value = idlistaprecioarticulo
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Eliminar el articulo de listaprecioarticulo_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idlistaprecioarticulo As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "listaprecioarticulo__Buscar"
                .Parameters.Add("@idlistaprecioarticulo", SqlDbType.Int).Value = idlistaprecioarticulo
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Buscar el articulo de listaprecioarticulo_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function BuscarXidarticuloXidlistaprecio(ByVal int_idarticulo As Integer, ByVal int_idlistaprecio As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            BuscarXidarticuloXidlistaprecio = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "listaprecioarticulo__BuscarXidarticuloXidlistaprecio"
                .Parameters.Add("@idarticulo", SqlDbType.Int).Value = int_idarticulo
                .Parameters.Add("@idlistaprecio", SqlDbType.Int).Value = int_idlistaprecio
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                BuscarXidarticuloXidlistaprecio = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Buscar el articulo de listaprecioarticulo_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function


    Public Function BuscarXidarticuloUltimoGENERAL(ByVal int_idarticulo As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            BuscarXidarticuloUltimoGENERAL = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "listaprecioarticulo__BuscarXidarticuloUltimoGENERAL"
                .Parameters.Add("@idarticulo", SqlDbType.Int).Value = int_idarticulo
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                BuscarXidarticuloUltimoGENERAL = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al BuscarXidarticuloUltimoGENERAL el articulo de listaprecioarticulo_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try

    End Function

    Public Function BuscarXidarticuloXidclienteUltimo(ByVal int_idarticulo As Integer, ByVal int_idcliente As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            BuscarXidarticuloXidclienteUltimo = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "listaprecioarticulo__BuscarXidarticuloXidclienteUltimo"
                .Parameters.Add("@idarticulo", SqlDbType.Int).Value = int_idarticulo
                .Parameters.Add("@idcliente", SqlDbType.Int).Value = int_idcliente
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                BuscarXidarticuloXidclienteUltimo = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al BuscarXidarticuloXidclienteUltimo el articulo de listaprecioarticulo_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try

    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "listaprecioarticulo__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarUltimo articulo de listaprecioarticulo_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "listaprecioarticulo__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarTodos los articulos de listaprecioarticulo_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_listaprecioarticulo_Clon As New cls_listaprecioarticulo_
        obj_listaprecioarticulo_Clon.int_idlistaprecioarticulo = Me.int_idlistaprecioarticulo
        obj_listaprecioarticulo_Clon.int_idlistaprecio = Me.int_idlistaprecio
        obj_listaprecioarticulo_Clon.int_idarticulo = Me.int_idarticulo
        obj_listaprecioarticulo_Clon.dbl_valor = Me.dbl_valor
        obj_listaprecioarticulo_Clon.int_idmoneda = Me.int_idmoneda
        obj_listaprecioarticulo_Clon.dec_costo = Me.dec_costo
        obj_listaprecioarticulo_Clon.str_formula = Me.str_formula
        obj_listaprecioarticulo_Clon.int_tipo = Me.int_tipo
        obj_listaprecioarticulo_Clon.col_Items = Me.col_Items
        Return obj_listaprecioarticulo_Clon
    End Function

    Function CargarXidlistaprecio(ByVal int_idlistaprecio As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXidlistaprecio = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "listaprecioarticulo__CargarXidlistaprecio"
                .Parameters.Add("@idlistaprecio", SqlDbType.Int).Value = int_idlistaprecio
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXidlistaprecio = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarXidlistaprecio los articulos de listaprecioarticulo_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Resolver() As Decimal
        Resolver = 0
        'Dim Control As New MSScriptControl.ScriptControl
        Dim tmp_formula As String = formula


        tmp_formula = Replace(formula, "[costo]", costo)
        tmp_formula = Replace(tmp_formula, ",", ".")

        'Control.Language = "VBScript"
        'Control.ExecuteStatement("Dim resultado" & vbNewLine & tmp_formula)
        'Return Math.Round(Control.Eval("resultado"), int_cantdecimales)

        'DevExpress.XtraEditors.Xtra Throw New Exception("Error de sintaxis, compruebe la formula.", "Formula", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End Function



End Class

